package test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

class TableDemo9 extends JFrame implements ActionListener
{
  JTable jt;

  TableDemo9 (String title)
  {
   // Pass the title to the JFrame superclass so that it appears in
   // the title bar.

   super (title);

   // Tell the program to exit when the user either selects Close
   // from the System menu or presses an appropriate X button on the
   // title bar.

   setDefaultCloseOperation (EXIT_ON_CLOSE);

   // Create a table with a default table model that specifies 10
   // rows by 10 columns dimensions.

   jt = new JTable (new DefaultTableModel (10, 10));

   // Register a RowListener object as a listener for selection
   // events originating from the row selection model.

   jt.getSelectionModel ().
     addListSelectionListener (new RowListener (jt));

   // Register a ColumnListener object as a listener for selection
   // events originating from the column selection model.

   jt.getColumnModel ().getSelectionModel ().
     addListSelectionListener (new ColumnListener (jt));

   // Add the table to the north portion of the frame window's
   // content pane.

   getContentPane ().add (jt, BorderLayout.NORTH);

   // Create a panel for positioning two panels of buttons.

   JPanel jp1 = new JPanel ();
   jp1.setLayout (new BorderLayout ());

   // Create a panel for positioning one row of buttons.

   JPanel jp2 = new JPanel ();

   // Create a "Row Selection Only" button, register the current
   // TableDemo9 object as a listener to that button's action
   // events, and add that button to the panel.

   JButton jb = new JButton ("Row Selection Only");
   jb.addActionListener (this);
   jp2.add (jb);

   // Create a "Column Selection Only" button, register the current
   // TableDemo9 object as a listener to that button's action
   // events, and add that button to the panel.

   jb = new JButton ("Column Selection Only");
   jb.addActionListener (this);
   jp2.add (jb);

   // Create a "Row and Column Selection" button, register the
   // current TableDemo9 object as a listener to that button's
   // action events, and add that button to the panel.

   jb = new JButton ("Row and Column Selection");
   jb.addActionListener (this);
   jp2.add (jb);

   // Add the jp2 panel to the north region of the jp1 panel.

   jp1.add (jp2, BorderLayout.NORTH);

   // Create a panel for positioning one row of buttons.

   jp2 = new JPanel ();

   // Create a "Single Selection" button, register the current
   // TableDemo9 object as a listener to that button's action
   // events, and add that button to the panel.

   jb = new JButton ("Single Selection");
   jb.addActionListener (this);
   jp2.add (jb);

   // Create a "Single Interval" button, register the current
   // TableDemo9 object as a listener to that button's action
   // events, and add that button to the panel.

   jb = new JButton ("Single Interval");
   jb.addActionListener (this);
   jp2.add (jb);

   // Create a "Multiple Intervals" button, register the current
   // TableDemo9 object as a listener to that button's action
   // events, and add that button to the panel.

   jb = new JButton ("Multiple Intervals");
   jb.addActionListener (this);
   jp2.add (jb);

   // Add the jp2 panel to the south region of the jp1 panel.

   jp1.add (jp2, BorderLayout.SOUTH);

   // Add the jp1 panel to the south portion of the frame window's
   // content pane.

   getContentPane ().add (jp1, BorderLayout.SOUTH);

   // Establish the frame's initial size as 600x275 pixels.

   setSize (600, 275);

   // Display the frame window and all contained components.

   setVisible (true);
  }

  public void actionPerformed (ActionEvent e)
  {
   // Identify the button that initiated the event.

   JButton jb = (JButton) e.getSource ();

   // Obtain the button's label.

   String label = jb.getText ();

   // Enable row and/or column selection, or set the selection mode,
   // as appropriate.

   if (label.equals ("Row Selection Only"))
   {
     jt.setRowSelectionAllowed (true);
     jt.setColumnSelectionAllowed (false);
   }
   else
   if (label.equals ("Column Selection Only"))
   {
     jt.setColumnSelectionAllowed (true);
     jt.setRowSelectionAllowed (false);
   }
   else
   if (label.equals ("Row and Column Selection"))
   {
     jt.setRowSelectionAllowed (true);
     jt.setColumnSelectionAllowed (true);
   }
   else
   if (label.equals ("Single Selection"))
     jt.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
   else
   if (label.equals ("Single Interval"))
     jt.setSelectionMode (ListSelectionModel.
                SINGLE_INTERVAL_SELECTION);
   else
     jt.setSelectionMode (ListSelectionModel.
                MULTIPLE_INTERVAL_SELECTION);

   // Remove the current selection to prevent confusion.

   jt.clearSelection ();
  }

  public static void main (String [] args)
  {
   // Create a TableDemo9 object, which creates the GUI.

   new TableDemo9 ("Table Demo #9");
  }
}

class ColumnListener implements ListSelectionListener
{
  private JTable jt;

  ColumnListener (JTable jt)
  {
   this.jt = jt;
  }

  public void valueChanged (ListSelectionEvent e)
  {
   if (e.getValueIsAdjusting ())
     return;

   int [] cols = jt.getSelectedColumns ();

   for (int i = 0; i < cols.length; i++)
      System.out.println ("Selected column = " + cols [i]);

   System.out.println ("CL: First index = " + e.getFirstIndex ());
   System.out.println ("CL: Last index = " + e.getLastIndex () +
             "\n");
  }
}

class RowListener implements ListSelectionListener
{
  private JTable jt;

  RowListener (JTable jt)
  {
   this.jt = jt;
  }

  public void valueChanged (ListSelectionEvent e)
  {
   if (e.getValueIsAdjusting ())
     return;

   int [] rows = jt.getSelectedRows ();

   for (int i = 0; i < rows.length; i++)
      System.out.println ("Selected row = " + rows [i]);

   System.out.println ("RL: First index = " + e.getFirstIndex ());
   System.out.println ("RL: Last index = " + e.getLastIndex () +
             "\n");
  }
}